import Vue from 'vue'
import VueRouter from 'vue-router'
import user_index from "@/views/user_index";
import u_index from "@/components/user/u_index";
import u_news from "@/components/user/u_news";
import u_company from "@/components/user/u_company";
import u_tourism from "@/components/user/u_tourism";
import u_feedback from "@/components/user/u_feedback";
import u_showInfo from "@/components/user/u_showInfo";
import u_companyInfo from "@/components/user/u_companyInfo";
import u_login from "@/views/u_login";
import u_tourismInfo from "@/components/user/u_tourismInfo";
import ArticleInfo from "@/components/admin/ArticleInfo";
import NewsInfo from "@/components/admin/NewsInfo";
import ArticleAdd from "@/components/admin/ArticleAdd";
import NewsAdd from "@/components/admin/NewsAdd";
import Main from "@/components/admin/Main";
import Index from "@/views/Index";
import Register from "@/views/Register";
import AirlineAdd from "@/components/admin/AirlineAdd";
import AirlineInfo from "@/components/admin/AirlineInfo";


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'u_login',
        component: u_login

    },
    {
        path: '/register',
        component: Register
    },
    {
        path: '/user_index',
        component: user_index,
        redirect: 'user_index/u_index',
        children: [
            {
                path: 'u_index',
                component: u_index
            },
            {
                path: 'u_news',
                component: u_news
            },
            {
                path: 'u_company',
                component: u_company
            },
            {
                path: 'u_tourism',
                component: u_tourism
            },
            {
                path: 'u_feedback',
                component: u_feedback
            },
            {
                path: 'u_showInfo',
                component: u_showInfo
            },
            {
                path: 'u_companyInfo',
                component: u_companyInfo
            },
            {
                path: 'u_tourismInfo',
                component: u_tourismInfo
            }
        ]
    },
    {
        path: '/Index',
        component: Index,
        children: [
            {
                path: 'Main',
                component: Main
            },
            {
                path: 'NewsAdd',
                component: NewsAdd
            },
            {
                path: 'ArticleAdd',
                component: ArticleAdd
            },
            {
                path: 'NewsInfo',
                component: NewsInfo
            },
            {
                path: 'ArticleInfo',
                component: ArticleInfo
            },
            {
                path: 'AirlineAdd',
                component: AirlineAdd
            },
            {
                path: 'AirlineInfo',
                component: AirlineInfo
            }
        ]
    },


]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //首先判断用户是否已经登录
    const token = JSON.parse(window.sessionStorage.getItem("token"));
    if (to.path === "/" || to.path === "/register"
    ) {
        next();
    } else {
        if (token) {
            //已经登录
            next();
        } else {
            next("/")
        }
    }
})

export default router
